package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.PanghuShop;
import com.hualan.dao.impl.MenDianDaoImpl;
import com.hualan.dao.MenDianDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.print.Book;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/MenDian/DataList")
public class MenDianListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端传递的分页信息
        String pageStr = request.getParameter("pageNum");
        int page = pageStr == null ? 1 : Integer.parseInt(pageStr);
        String sizeStr = request.getParameter("pageSize");
        int size = sizeStr == null ? 5 : Integer.parseInt(sizeStr);
//        System.out.println(page);
//        System.out.println(size);
        //2.创建Map对象，用来给前端返回信息
        HashMap<String, Object> map = new HashMap<>();
        //3.查询数据的总条数---增删改查来自于dao
        //4.将数量存储道map中
        //5.按照分页查询数据
        MenDianDao menDianDao = new MenDianDaoImpl();
        List<PanghuShop> shopList = menDianDao.queryMenDianWithPage(page, size);
        //拿到数据库数据的总条数
        Long count = menDianDao.selectMenDinCount();
        map.put("MenDianCount", count);
//        System.out.println(booksList+"1111111111111111111");
        //将数据以键值对 存储在map中
        map.put("list", shopList);
        //6.设置code吗 200表示成功 300失败 以及message信息
        if (shopList != null) {
            map.put("code", 200);
            map.put("message", "数据获取成功");
        } else {
            map.put("code", 300);
            map.put("message", "数据获取失败！！");
        }
        //7.将数据map 通过reponse返回给前端 问题？
        String JSONString = JSON.toJSONString(map);
        response.getWriter().write(JSONString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
